import pytest
import requests
from data.ihrm_data import Inquire_data


class IhrmLoginApi:
    session = None

    @classmethod
    def ihrm_login(cls, json_body):
        # 创建一个会话
        sess = requests.Session()
        # 调用会话的post方法请求发送数据,行头体,知道是返回什么格式头可以不用写
        # 把获取的请求赋值给类属性
        cls.session = sess.post(url="http://ihrm-test.itheima.net/api/sys/login",
                                json=json_body)
        # 把类属性赋值给body_url,并返回给调用者
        body_url = cls.session
        return body_url


class IhrmStaffAddApi:
    session = None

    @classmethod
    def ihrm_staff_add(cls, json_doby):
        data_body = {"mobile": "13800000002", "password": "123456"}
        data_json = IhrmLoginApi.ihrm_login(data_body)
        token = "Bearer " + data_json.json()["data"]
        sess = requests.Session()
        cls.session = sess.post(url="http://ihrm-test.itheima.net/api/sys/user",
                                headers={"Authorization": token},
                                json=json_doby)
        body_url = cls.session

        return body_url


class IhrmInquire:
    session = None

    @classmethod
    def ihrm_inquire(cls, data_id):
        data_body = {"mobile": "13800000002", "password": "123456"}
        data_json = IhrmLoginApi.ihrm_login(data_body)
        token = "Bearer " + data_json.json()["data"]
        sess = requests.Session()
        cls.session = sess.get(url=f"http://ihrm-test.itheima.net/api/sys/user/{data_id}",
                               headers={"Authorization": token})
        body_url = cls.session

        return body_url


class IhrmModify:
    session = None

    @classmethod
    def ihrm_modify(cls, json_body, data_id):
        data_body = {"mobile": "13800000002", "password": "123456"}
        data_json = IhrmLoginApi.ihrm_login(data_body)
        token = "Bearer " + data_json.json()["data"]
        sess = requests.Session()
        cls.session = sess.put(url=f"http://ihrm-test.itheima.net/api/sys/user/{data_id}",
                               headers={"Authorization": token},
                               json=json_body)

        body_url = cls.session

        return body_url


class IhrmDelete:
    session = None

    @classmethod
    def ihrm_delete(cls, data_id):
        data_body = {"mobile": "13800000002", "password": "123456"}
        data_json = IhrmLoginApi.ihrm_login(data_body)

        token = "Bearer " + data_json.json()["data"]

        sess = requests.Session()
        cls.session = sess.delete(url=f"http://ihrm-test.itheima.net/api/sys/user/{data_id}",
                                  headers={"Authorization": token})
        body_url = cls.session

        return body_url
